package com.link.base.base.expenseapplication.service;

import com.link.base.base.expenseapplication.dao.mybatis.mapper.ApplicationProductMapper;
import com.link.base.base.expenseapplication.model.ApplicationProduct;
import com.link.core.basic.dao.mapper.BasicMapper;
import com.link.core.basic.service.BasicServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
 * 费用申请-门店-产品-费用申请-门店-产品表
 *
 * @author 黄正杨
 * @date 2019/04/16 19:06
 */
@Service
public class ApplicationProductServiceImpl extends BasicServiceImpl<ApplicationProduct> implements ApplicationProductService {

    @Resource
    private ApplicationProductMapper applicationProductMapper;

    @Override
    public BasicMapper<ApplicationProduct> getBasicMapper() {
        return applicationProductMapper;
    }

    @Override
    public List<ApplicationProduct> queryByExamplePage(ApplicationProduct entity) throws Exception {
        return computeMoney(super.queryByExamplePage(entity));
    }

    @Override
    public List<ApplicationProduct> computeMoney(List<ApplicationProduct> list) {
        if (CollectionUtils.isEmpty(list)) {
            return list;
        }
        BigDecimal productPrice;
        BigDecimal amount;
        BigDecimal totalPrice;
        for (ApplicationProduct item : list) {
            //单价如果为空则取0
            productPrice = null == item.getProductPrice() ? BigDecimal.ZERO : item.getProductPrice();
            //数量如果为空则取0
            amount = null == item.getAmount() ? BigDecimal.ZERO : new BigDecimal(item.getAmount());
            //申请总额 = 单价*数量
            totalPrice = productPrice.multiply(amount);
            //经销商承担金额计算逻辑
            if (null != item.getApplyPrice()) {
                item.setAccountAmount(item.getApplyPrice().multiply(amount));
            } else if (null != item.getApplyDiscount()) {
                item.setAccountAmount(totalPrice.multiply(item.getApplyDiscount()));
            } else {
                item.setAccountAmount(totalPrice);
            }
            //公司支持金额 = 申请总额 - 经销商承担费用
            item.setCompanyAmount(totalPrice.subtract(item.getAccountAmount()));
            item.setTotalPrice(totalPrice);
        }
        return list;
    }
}

